﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;
namespace Lists
{
    public class NhanVienDieuTraGiamSat : DoiTuong
    {
        #region HoTen property
        public string HoTen
        {
            get
            {
                if (_DataRow["HoTen"] is string) return (string)_DataRow["HoTen"];
                return "";
            }
            set
            {
                _DataRow["HoTen"] = value;
            }
        }
        #endregion

        #region GioiTinh property
        public bool GioiTinh
        {
            get
            {
                return (bool)_DataRow["GioiTinh"];
            }
            set
            {
                _DataRow["GioiTinh"] = value;
            }
        }
        #endregion

        #region NamSinh property
        public int NamSinh
        {
            get
            {
                if ((int)_DataRow["NamSinh"] is int) return (int)_DataRow["NamSinh"];
                else return 0;
            }
            set
            {
                _DataRow["NamSinh"] = value;
            }
        }
        #endregion

        #region Tuoi property
        public int Tuoi
        {
            get
            {
                if ((int)_DataRow["Tuoi"] is int) return (int)_DataRow["Tuoi"];
                else return 0;
            }
            set
            {
                _DataRow["Tuoi"] = value;
            }
        }
        #endregion

        #region DienThoai property
        public string DienThoai
        {
            get
            {
                if (_DataRow["DienThoai"] is string) return (string)_DataRow["DienThoai"];
                return "";
            }
            set
            {
                _DataRow["DienThoai"] = value;
            }
        }
        #endregion

        public NhanVienDieuTraGiamSat(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucNhanVienDieuTraGiamSat : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucNhanVienDieuTraGiamSat _Instance;
        public static DanhMucNhanVienDieuTraGiamSat Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucNhanVienDieuTraGiamSat();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "NhanVienDT_GS";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new NhanVienDieuTraGiamSat((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public NhanVienDieuTraGiamSat this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new NhanVienDieuTraGiamSat(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public NhanVienDieuTraGiamSat NewDoiTuong()
        {
            return new NhanVienDieuTraGiamSat(DataTable.NewRow());
        }

        public NhanVienDieuTraGiamSat GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new NhanVienDieuTraGiamSat(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public NhanVienDieuTraGiamSat GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);;
        }

        public static DanhMucNhanVienDieuTraGiamSat GetAll()
        {
            return Instance;
        }

        public static DanhMucNhanVienDieuTraGiamSat Filter(string filterExpression)
        {
            try
            {
                DanhMucNhanVienDieuTraGiamSat obj = new DanhMucNhanVienDieuTraGiamSat();
                obj.DataView = new DataView(DanhMucNhanVienDieuTraGiamSat.Instance.DataTable, filterExpression, "Ten", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static NhanVienDieuTraGiamSat GetByHoTen(string hoTen)
        {
            try
            {
                return DanhMucNhanVienDieuTraGiamSat.Instance.GetByProperty("HoTen", hoTen);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static NhanVienDieuTraGiamSat GetBySDT(string sdt)
        {
            try
            {
                return DanhMucNhanVienDieuTraGiamSat.Instance.GetByProperty("DienThoai", sdt);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
